import numpy as np
import math as mt
X0 = [680, 670, 655]
# 累加数列
X1 = [X0[0]]
add = X0[0] + X0[1]
X1.append(add)
i = 2
while i < len(X0):
    add = add + X0[i]
    X1.append(add)
    i += 1
# 紧邻均值序列
Z = []
j = 1
while j < len(X1):
    num = (X1[j] + X1[j - 1]) / 2
    Z.append(num)
    j = j + 1
# 最小二乘法计算
Y = []
x_i = 0
while x_i < len(X0) - 1:
    x_i += 1
    Y.append(X0[x_i])
Y = np.mat(Y)
Y = Y.reshape(-1,1)
B = []
b = 0
while b < len(Z):
    B.append(-Z[b])
    b += 1
B = np.mat(B)
B = B.reshape(-1,1)
c = np.ones((len(B),1))
B = np.hstack((B,c))
print("B",B)
# 求出参数
alpha = np.linalg.inv(B.T.dot(B)).dot(B.T).dot(Y)
a = alpha[0,0]
b = alpha[1,0]
print('alpha',alpha)
print("a=",a)
print("b=",b)
# 生成预测模型
GM = []
GM.append(X0[0])
did = b/a
k = 1
while k < len(X0):
    GM.append((X0[0] - did) * mt.exp(-a * k) + did)
    k += 1
# 做差得到预测序列
G = []
G.append(X0[0])
g = 1
while g < len(X0):
    G.append(round(GM[g] - GM[g - 1]))
    g += 1
print("预测数列为：",G)